import Types from '../../action/types'
const defaultState = {};

/**
 * 1.节点树
 * popular: {
 *          java:{
 *              items:[],
 *              isLoading:false
 *          },
 *          ios:{
 *              items:[],
 *              isLoading:false
 *          },
 *          ...
 * }
 * 2.动态的设置store数，横向拓展，以及动态获取store（难点：storeKey不固定）
 * @param {*} state 
 * @param {*} action 
 * @returns 
 */
export default function onAction(state = defaultState, action) {
    switch (action.type) {
        case Types.POPULAR_REFRESH_SUCCESS: // 下拉刷新成功
            return {
                ...state, // 只能返回一个新的state，不能对原有state进行修改
                [action.storeName]: {
                    ...state[action.storeName],
                    items: action.items, // 原始数据
                    projectModel: action.projectModel, // 此次要展示的数据
                    isLoading: false,
                    hideLoadingMore: false,
                    pageIndex: action.pageIndex,
                },
            };
        case Types.POPULAR_REFRESH: // 下拉刷新
            return {
                ...state,
                [action.storeName]: {
                    ...state[action.storeName],
                    isLoading: true,
                    hideLoadingMore: true,
                },
            };
        case Types.POPULAR_REFRESH_FAIL: // 下拉刷新失败
            return {
                ...state,
                [action.storeName]: {
                    ...state[action.storeName],
                    isLoading: false,
                },
            };
        case Types.POPULAR_LOAD_MORE_SUCCESS: // 上拉加载更多成功
            return {
                ...state,
                [action.storeName]: {
                    ...state[action.storeName],
                    projectModel: action.projectModel,
                    hideLoadingMore: false,
                    pageIndex: action.pageIndex,
                },
            };
        case Types.POPULAR_LOAD_MORE_FAIL: // 上拉加载更多失败
            return {
                ...state,
                [action.storeName]: {
                    ...state[action.storeName],
                    hideLoadingMore: true,
                    pageIndex: action.pageIndex,
                },
            };
        case Types.FLUSH_POPULAR_FAVORITE: // 刷新收藏状态
            return {
                ...state,
                [action.storeName]: {
                    ...state[action.storeName],
                    projectModel: action.projectModel,
                },
            };
        default:
            return state;
    }
}